package com.demiroot.server.pages;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.demiroot.server.Cookie;
import com.demiroot.server.HTTPResponse;
import com.demiroot.server.HeaderData;
import com.demiroot.server.ServerUtils;


public class JavaPageResponse implements HTTPResponse {
	private int returnCode;
	private boolean needsHeaders;
	private Object message;
	private ResponseType responceType;
	private HeaderData header;
	private List<Cookie> cookies = new ArrayList<Cookie>();
	private Calendar cookiesExpire;
	
	public JavaPageResponse(String mes) {
		returnCode = 200;
		needsHeaders = true;
		message = mes;
		responceType = ResponseType.STRING;
		header = null;
	}
	
	public JavaPageResponse(byte[] mes) {
		returnCode = 200;
		needsHeaders = true;
		message = mes;
		responceType = ResponseType.BYTEARRAY;
		header = null;
	}
	
	public void addCookie(Cookie c) {
		cookies.add(c);
	}
	
	public void setCookiesExpires(Calendar expire) {
		cookiesExpire = expire;
	}

	public int getCode() {
		return returnCode;
	}

	public HeaderData getHeader() {
		return header;
	}

	public Object getMessage() {
		return message;
	}

	public ResponseType getResponseType() {
		return responceType;
	}

	public byte[] getMessageAsByteArray() {
		if (responceType == ResponseType.BYTEARRAY) {
			return (byte[])message;
		} else if (responceType == ResponseType.STRING) {
			return ServerUtils.stringToByteArray((String)message);
		}
		return null;
	}

	@Override
	public List<Cookie> getCookies() {
		return cookies;
	}

	@Override
	public Calendar cookieExpire() {
		return cookiesExpire;
	}
	
	
}
